package fun.diaoer.mapper.provider;

import org.apache.ibatis.mapping.MappedStatement;
import tk.mybatis.mapper.mapperhelper.MapperHelper;
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
import tk.mybatis.mapper.mapperhelper.SqlHelper;

public class DIBaseDeleteProvider extends MapperTemplate{
	
	public DIBaseDeleteProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
		super(mapperClass, mapperHelper);
	}
	
	/**
	 * 获取数据库当前时间
	 */
	public String getDatabaseDate(MappedStatement ms) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select CURDATE() ");
		return sql.toString();
	}

	public String getDatabaseTimestamp(MappedStatement ms) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select now() ");
		return sql.toString();
	}

	public String deleteById(MappedStatement ms){
		final Class<?> entityClass = getEntityClass(ms);
		StringBuilder sql = new StringBuilder();
		sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
		// 无乐观锁
		sql.append(SqlHelper.wherePKColumns(entityClass, false));
		return sql.toString();
	}
	
	/**
	 * 通过主键删除（乐观锁-新版本删除不支持乐观锁了）
	 *
	 * @param ms
	 */
	public String deleteByPrimaryKey2(MappedStatement ms) {
		final Class<?> entityClass = getEntityClass(ms);
		StringBuilder sql = new StringBuilder();
		sql.append(SqlHelper.deleteFromTable(entityClass, tableName(entityClass)));
		// 增加 @Version 乐观锁支持
		sql.append(SqlHelper.wherePKColumns(entityClass, true));
		return sql.toString();
	}
	
}
